/*
 * Copyright (c) 2022-2022 All rights Reserved.
 */

package com.driving.core.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.driving.core.base.BaseController;
import com.driving.core.base.IPersonController;
import com.driving.core.entity.Coach;
import com.driving.core.mapper.type.Gender;
import com.driving.core.service.ICoachService;
import com.driving.core.service.dto.CoachDTO;
import com.driving.core.util.Constant;
import com.driving.core.util.Constant.Audience;
import com.driving.core.util.MessageType.ExceptionType;
import com.driving.core.util.Response;

/**
 * @since 2017
 */
@RestController
@RequestMapping(value = "/coach")
public class CoachController extends BaseController<ICoachService, Coach> implements IPersonController<CoachDTO> {

    @GetMapping(value = "/my-student")
    public Response<List<Map<String, Object>>> myStudent(HttpServletRequest request) {
        int coachId = getNonNullCorrespondingIdByRequest(request.getHeader(Constant.TOKEN), Audience.COACH);
        return Response.success(baseService.myStudent(coachId));
    }

    @GetMapping(value = "/student-detail/{studentId}")
    public Response<Map<String, Object>> studentDetail(HttpServletRequest request, @PathVariable int studentId) {
        int coachId = getNonNullCorrespondingIdByRequest(request.getHeader(Constant.TOKEN), Audience.COACH);
        return Response.success(baseService.studentDetail(coachId, studentId));
    }

    @Override
    @PostMapping(value = "/password-update")
    public Response<Boolean> passwordUpdate(HttpServletRequest request, @RequestBody CoachDTO coachDTO) {
        if (validateAllParams(coachDTO.getOldPassword(), coachDTO.getPassword())) {
            return Response.failure(configReader.getExceptionMessage(ExceptionType.NOT_COMPLETE));
        }
        String token = request.getHeader(Constant.TOKEN);
        return Response.success(baseService.passwordUpdateOrReset(coachDTO, token));
    }

    @Override
    @PostMapping(value = "/password-reset")
    public Response<Boolean> passwordReset(@RequestBody CoachDTO dto) {
        if (validateAllParams(dto.getMobile(), dto.getPassword())) {
            return Response.failure(configReader.getExceptionMessage(ExceptionType.NOT_COMPLETE));
        }
        return Response.success(baseService.passwordUpdateOrReset(dto, null));
    }

    // @PostMapping(value = "/register")
    public Response<Object> register(@RequestBody CoachDTO coachDTO) {
        // configReader.getExceptionMessage(ExceptionType.OPERATION_NOT_SUPPORTED)
        String name = coachDTO.getName();
        Gender gender = coachDTO.getGender();
        String mobile = coachDTO.getMobile();
        String password = coachDTO.getPassword();
        String correctType = coachDTO.getCorrectType();
        if (!validateAllParams(name, gender, mobile, password, correctType)) {
            return Response.failure(configReader.getExceptionMessage(ExceptionType.NOT_COMPLETE));
        }
        return Response.success(baseService.register(coachDTO));
    }

    @Override
    @PostMapping(value = "/login")
    public Response<Map<String, String>> login(@RequestBody CoachDTO coachDTO) {
        if (!validateAllParams(coachDTO.getMobile(), coachDTO.getPassword())) {
            return Response.failure(configReader.getExceptionMessage(ExceptionType.NOT_COMPLETE));
        }
        return Response.success(baseService.login(coachDTO));
    }
}
